/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.Enum.DistributionTypeEnum;
import com.ykht.proj.coupon.service.FxConfRewardService;
import com.ykht.proj.coupon.vo.FxConfRewardVO;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;


/**
 * 品牌邀请、转发、核销配置表
 *
 * @author proj code generator
 * @date 2021-02-01 19:00:18
 */
@RestController
@AllArgsConstructor
@RequestMapping("/fxconfreward")
@Api(value = "fxconfreward", tags = "品牌邀请、转发、核销配置表管理")
public class FxConfRewardController {

	private final FxConfRewardService fxConfRewardService;

	/**
	 * 分页查询
	 *
	 * @param page           分页对象
	 * @param fxConfRewardVO 品牌邀请、转发、核销配置表
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	//@PreAuthorize("@pms.hasPermission('coupon_fxconfreward_view')" )
	public R getFxConfRewardPage(Page page, FxConfRewardVO fxConfRewardVO) {
		IPage<FxConfRewardVO> iPage = fxConfRewardService.queryByPage(page, fxConfRewardVO);
		return R.ok(iPage);
	}


	/**
	 * 通过id查询品牌邀请、转发、核销配置表
	 *
	 * @param shopId 门店id
	 * @return R
	 */
	@ApiOperation(value = "配置门店品牌信息", notes = "配置门店品牌信息")
	@GetMapping("/findById")
	//@PreAuthorize("@pms.hasPermission('coupon_fxconfreward_view')" )
	public R getById(Long shopId) {
		return R.ok(fxConfRewardService.findById(shopId));
	}

	@ApiOperation(value = "配置记录", notes = "配置记录")
	@GetMapping("/findListById")
	//@PreAuthorize("@pms.hasPermission('coupon_fxconfreward_view')" )
	public R findListById(Page page, FxConfRewardVO fxConfRewardVO) {
		fxConfRewardService.updateStatus();
		DistributionTypeEnum distributionTypeEnum = DistributionTypeEnum.getThisByDesc(fxConfRewardVO.getFenXiaoName());
		if (distributionTypeEnum != null) fxConfRewardVO.setRuleType(distributionTypeEnum.getValue());//分线任务文字搜索
		IPage<FxConfRewardVO> iPage = fxConfRewardService.findListById(page, fxConfRewardVO);
		return R.ok(iPage);
	}

	@ApiOperation(value = "品牌和门店", notes = "品牌和门店")
	@GetMapping("/findShopAndBrand")
	//@PreAuthorize("@pms.hasPermission('coupon_fxconfreward_view')" )
	public R findShopAndBrand(Long shopId) {
		return R.ok(fxConfRewardService.findShopAndBrand(shopId));
	}

	/**
	 * 新增品牌邀请、转发、核销配置表
	 *
	 * @param fxConfRewardVO 品牌邀请、转发、核销配置表
	 * @return R
	 */
	@ApiOperation(value = "新增品牌邀请、转发、核销配置表", notes = "新增品牌邀请、转发、核销配置表")
	@SysLog("新增品牌邀请、转发、核销配置表")
	@PostMapping
//    @PreAuthorize("@pms.hasPermission('coupon_fxconfreward_add')" )
	public R save(@RequestBody FxConfRewardVO fxConfRewardVO) {
		fxConfRewardVO.setCreateUser(SecurityUtils.getUserId());
		fxConfRewardVO.setCreateTime(LocalDateTime.now());
		return R.ok(fxConfRewardService.insert(fxConfRewardVO));
	}

	/**
	 * 修改品牌邀请、转发、核销配置表
	 *
	 * @param fxConfRewardVo 品牌邀请、转发、核销配置表
	 * @return R
	 */
	@ApiOperation(value = "修改品牌邀请、转发、核销配置表", notes = "修改品牌邀请、转发、核销配置表")
	@SysLog("修改品牌邀请、转发、核销配置表")
	@PutMapping
//    @PreAuthorize("@pms.hasPermission('coupon_fxconfreward_edit')" )
	public R updateById(@RequestBody List<FxConfRewardVO> fxConfRewardVo) {
		return R.ok(fxConfRewardService.updateConf(fxConfRewardVo));
	}

	/**
	 * 通过id删除品牌邀请、转发、核销配置表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除品牌邀请、转发、核销配置表", notes = "通过id删除品牌邀请、转发、核销配置表")
	@SysLog("通过id删除品牌邀请、转发、核销配置表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxconfreward_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(fxConfRewardService.removeById(id));
	}
}
